package com.iwuke.dance.wallet;

import android.view.View;
import android.widget.TextView;
import com.iwuke.dance.R;
import com.iwuke.dance.activity.BaseActivity;
import com.iwuke.dance.gadget_view.TitleBarView;
import com.iwuke.dance.http_request.Bean;
import com.iwuke.dance.http_request.PathUrl;
import com.iwuke.dance.util.PhoneMaskUtil;
import com.iwuke.dance.util.ToastUtil;
import com.iwuke.dance.util.Util;
import com.iwuke.dance.util.independent_edit_text.IndependentEditTextUtil;
import com.iwuke.dance.util.merchants_connect.NetworkEvent;

import java.util.HashMap;
import java.util.Map;

import de.greenrobot.event.Subscribe;
import de.greenrobot.event.ThreadMode;

/**
 * @author: 魏从响
 * @date: 2025/9/15
 * 输入验证码 Enter Verification Code
 */
public class EnterVerificationCodeActivity extends BaseActivity implements View.OnClickListener {

    private TitleBarView title_bar;
    private long time = 59;
    private IndependentEditTextUtil independentEditTextUtil;
    private String phone_number;

    /**
     * @description 网络实时监听
     */
    @Subscribe(threadMode = ThreadMode.MainThread)
    public void onEvent(NetworkEvent networkEvent){
        BaseActivity.setConnect(EnterVerificationCodeActivity.this,networkEvent,title_bar);

    }

    @Override
    protected int getView() {
        return R.layout.activity_enter_verification_code;
    }

    @Override
    protected void init(View mView) {
        title_bar = findViewById(R.id.title_bar);

        independentEditTextUtil = new IndependentEditTextUtil(this, mView);
    }

    @Override
    protected void initData() {
        //电话号码
        TextView tv_phone_number = findViewById(R.id.tv_phone_number);
        String phone = getIntent().getStringExtra("text");
        phone_number = phone.substring(3);//从第三个索引开始（包含3），剔除前缀（例：“+86”）；
        tv_phone_number.setText("验证码已发送"+ PhoneMaskUtil.maskPhoneNumber(phone_number));

        independentEditTextUtil.setOnEditTextClick(new IndependentEditTextUtil.OnEditTextClick() {
            @Override
            public void onEditTextClick(String value) {
                //type: 3 换绑银行卡；4 绑定银行卡；5 重置支付密码；6 修改支付密码；7 钱包开通
                Map<String, Object> map = new HashMap<>();
                map.put("type",7);
                map.put("phoneNum",phone_number);//此处没有前缀“+86”
                map.put("verifyCode",value);

                PathUrl.verifySMS(map, new PathUrl.DataCallBack<Bean>() {
                    @Override
                    public void onSucess(Bean bean) {

                        //绑定本人身份信息
                        Util.setIntent(EnterVerificationCodeActivity.this, IdentityInformationActivity.class);
                        finish();
                    }

                    @Override
                    public void onFail(String errorCode) {
                        ToastUtil.setToast(EnterVerificationCodeActivity.this,errorCode);
                    }
                });

            }

            @Override
            public void onResendTimeClick() {
                sendSMS();//发送验证码请求
            }
        });

        sendSMS();//发送验证码请求

        title_bar.setLeftOnClick(this);
        title_bar.setTitleText(null);
    }

    /**
     * @description 发送验证码请求
     * @param //type: 3 换绑银行卡；4 绑定银行卡；5 重置支付密码；6 修改支付密码；7 钱包开通
     * @return
     * @author 魏从响
     * @time 2025/10/9 14:26
     */
    private void sendSMS(){
        Map<String, Object> map = new HashMap<>();
        map.put("type",7);
        map.put("phoneNum",phone_number);
        PathUrl.sendSMS(map, new PathUrl.DataCallBack<Bean>() {
            @Override
            public void onSucess(Bean bean) {//429
                if (bean!=null){
                    time = bean.id;
                    independentEditTextUtil.resendTime((int) time);
                }else {
                    independentEditTextUtil.startResendTimer();
                    ToastUtil.setToast(EnterVerificationCodeActivity.this,"发送成功");
                }
            }

            @Override
            public void onFail(String errorCode) {
                ToastUtil.setToast(EnterVerificationCodeActivity.this,errorCode);
            }
        });
    }

    @Override
    public void onClick(View v) {
        if (v.getId()==R.id.img_left){
            finish();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (independentEditTextUtil!=null){
            independentEditTextUtil.onDestroy();
        }
        if (independentEditTextUtil!=null){
            independentEditTextUtil=null;
        }
    }
}
